package com.taotao.manage.converter;

import org.springframework.core.convert.converter.Converter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateConverter implements Converter<String,Date> {
    private String[] patterns = new String[] {"yyyy-MM-dd HH:mm:ss","yyyy年MM月dd日 HH:mm:ss", "yyyy-MM-dd HH:mm", "HH:mm", "yyyy-MM-dd" };
    @Override
    public Date convert(String source) {
        for (int i = 0; i < patterns.length; i++) {
            if (patterns[i].length() == source.length()) {
                SimpleDateFormat format = new SimpleDateFormat((patterns[i]).toString());
                try {
                    return format.parse(source);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }
}
